#include "defectform.h"
#include "ui_defectform.h"

#include <QGraphicsDropShadowEffect>
#include <QMessageBox>

#include <Dlg/defectdlg.h>
#include <Dlg/plandlg.h>

DefectForm::DefectForm(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::DefectForm)
{
    ui->setupUi(this);
    m_sql.init("DefectForm_data");
    InitPage();
    UpdateTable();

}

DefectForm::~DefectForm()
{
    delete ui;
}

void DefectForm::InitPage()
{
    ui->tableWidget->setShowGrid(true);
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);

    //自动调整窗口列表的【宽、高】---适应窗口大小
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    ui->tableWidget->verticalHeader()->hide();//取消默认行号
    QStringList header;
    header << tr("Index")<< tr("defect_config")<< tr("createtime")<< tr("changetime");
    ui->tableWidget->setColumnCount(header.size());
    ui->tableWidget->setHorizontalHeaderLabels(header);

    m_nCurPage = 1;
    m_nRowsPerPage = 7;
    m_nTotalPages = 1;

    ui->Bt_Change->setEnabled(false);
    ui->Bt_Delete->setEnabled(false);
    ui->Bt_Search->setEnabled(false);

    connect(ui->tableWidget->verticalScrollBar(), SIGNAL(valueChanged(int)),this,SLOT(OnSlideAction(int)));//响应滚动条翻页

    ui->Bt_Return->hide();


    QGraphicsDropShadowEffect *shadow_effectA = new QGraphicsDropShadowEffect(this);
    QGraphicsDropShadowEffect *shadow_effectB = new QGraphicsDropShadowEffect(this);

    shadow_effectA->setOffset(0, 0);
    shadow_effectA->setColor(QColor(150,150,150));
    shadow_effectA->setBlurRadius(6);

    shadow_effectB->setOffset(0, 0);
    shadow_effectB->setColor(QColor(150,150,150));
    shadow_effectB->setBlurRadius(6);


    ui->defect_gBox_1->setGraphicsEffect(shadow_effectA);
    ui->defect_gBox_2->setGraphicsEffect(shadow_effectB);

    //允许绘制背景色交替 必须写这句否则交替背景色没有效果
    ui->tableWidget->setAlternatingRowColors(true);
}

void DefectForm::UpdateTable()
{
    QList<DefectScheme> recoders = m_sql.queryAllDefectSchemes();
    ui->tableWidget->clearContents();
    if(recoders.size()%m_nRowsPerPage ==0){
        m_nTotalPages = recoders.size()/m_nRowsPerPage;
    }else{
        m_nTotalPages = recoders.size()/m_nRowsPerPage+1;
    }
    ui->labPage->setText(QString("%1/%2").arg(m_nCurPage).arg(m_nTotalPages));
    ui->tableWidget->setRowCount(recoders.size()); // 设置显示的行数
    for(int i=0;i<recoders.size();i++){
        ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString::number(recoders[i].schemeId)));
        ui->tableWidget->setItem(i,1,new QTableWidgetItem(recoders[i].schemeName));
        ui->tableWidget->setItem(i,2,new QTableWidgetItem(recoders[i].createTime));
        ui->tableWidget->setItem(i,3,new QTableWidgetItem(recoders[i].changeTime));
        for(int j=0;j<4;j++){
            ui->tableWidget->item(i,j)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        }
    }
}

void DefectForm::slotsDlgInfo()
{
    UpdateTable();
}

void DefectForm::on_Bt_PrePage_clicked()
{
    m_nCurPage--;
    if(m_nCurPage <=0){
        m_nCurPage = m_nTotalPages;
    }
    ui->tableWidget->verticalScrollBar()->setSliderPosition(m_nCurPage*m_nRowsPerPage - m_nRowsPerPage);
    ui->labPage->setText(QString("%1/%2").arg(m_nCurPage).arg(m_nTotalPages));
}

void DefectForm::on_Bt_NextPage_clicked()
{
    m_nCurPage++;
    if (m_nCurPage > m_nTotalPages)
    {
        m_nCurPage = 1;
    }

    ui->tableWidget->verticalScrollBar()->setSliderPosition(m_nCurPage*m_nRowsPerPage - m_nRowsPerPage);
    ui->labPage->setText(QString("%1/%2").arg(m_nCurPage).arg(m_nTotalPages));

}

void DefectForm::OnSlideAction(int nRowIndex)
{

    ++nRowIndex;
    //计算当前行是那一页
    if (nRowIndex % m_nRowsPerPage == 0)
    {
        m_nCurPage = nRowIndex / m_nRowsPerPage;
    }
    else
    {
        m_nCurPage = nRowIndex / m_nRowsPerPage+1;
    }
    ui->labPage->setText(QString("%1/%2").arg(m_nCurPage).arg(m_nTotalPages));

}

void DefectForm::on_Bt_Add_clicked()
{
    DefectDlg* pDefect = new DefectDlg();
    pDefect->iniPage(1);
    pDefect->show();
    connect(pDefect, &DefectDlg::sigFormInfo, this, &DefectForm::slotsDlgInfo);
}

void DefectForm::on_Bt_Change_clicked()
{
    DefectDlg* pDefect = new DefectDlg();
    pDefect->iniPage(3);
    pDefect->show();
    pDefect->upPageData(m_strCurID.toInt());
    connect(pDefect, &DefectDlg::sigFormInfo, this, &DefectForm::slotsDlgInfo);
}

void DefectForm::on_Bt_Search_clicked()
{
    DefectDlg* m_dSearch = new DefectDlg();
    m_dSearch->iniPage(2);
    m_dSearch->show();
    m_dSearch->upPageData(m_strCurID.toInt());
}

void DefectForm::on_Bt_Delete_clicked()
{
    if (!(QMessageBox::question(this, tr("Delete"), tr("Whether to delete the current row?"), tr("Yes"), tr("No"))))
    {
        m_sql.deleteDefectschemes(m_strCurID.toInt());
        UpdateTable();
    }
}

void DefectForm::on_tableWidget_cellClicked(int row, int column)
{
    column = 0;
    if(m_strCurID.isEmpty()){
        ui->Bt_Change->setEnabled(true);
        ui->Bt_Delete->setEnabled(true);
        ui->Bt_Search->setEnabled(true);
    }
    QTableWidgetItem* item = ui->tableWidget->item(row ,0);
    m_strCurID = item->text();
}



void DefectForm::on_tableWidget_cellDoubleClicked(int row, int column)
{
    column = row;
    on_Bt_Search_clicked();
}
